home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1640 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: news.mel.aone.net.au!usenet
  2. From: clyde@hitech.com.au (Clyde Smith-Stubbs)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What the hell is THIS?! - afile (1/1)
  5. Date: Mon, 15 Jan 1996 22:14:13 GMT
  6. Organization: HI-TECH Software
  7. Message-ID: <30fad19c.85098432@news.bne.aone.net.au>
  8. References: <4d6rgh$rfu@abel.cc.sunysb.edu>
  9. Reply-To: clyde@hitech.com.au
  10. NNTP-Posting-Host: skyhawk.hitech.com.au
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. In 12 Jan 1996 23:40:01 GMT, bmadhusu@engws12.ic.sunysb.edu
  14. (Bommasamudram Madhusudan) wrote:
  15.  
  16. >Can someone explain what
  17. >
  18. >  int (*p)[3]  is?????
  19. >
  20. >  Is this an array of pointers to integers 
  21. >
  22. >  OR
  23. >
  24. >  a pointer to an array of integers?? HELP!
  25.  
  26. Well, in spite of what someone else said in the newsgroup, this is a
  27. pointer to an array of integers. There are two ways to figure out
  28. difficult declarations like this; the classical way is to work out
  29. what happens when you use the variable, remembering that the syntax of
  30. declarations is based on the syntax of expressions. So in an
  31. expression
  32.  
  33.     (*p)[0]
  34.  
  35. will firstly dereference p, then index the result. This means p is a
  36. pointer to an array. The other way is to follow the following rules:
  37.  
  38. Read the declaration's innermost parentheses first;
  39. Read from right to left.
  40.  
  41. The declaration was:
  42.  
  43. int    (*p)[3];
  44.  
  45. So in this case we first read inside the parens - start with 'p',
  46. "p is"; then read the '*', so "p is a pointer". Now step outside the
  47. parens, and go as far right as we can and start reading back to the
  48. left again:
  49.  
  50. We read: "p * [3] int"
  51.  
  52. "p is a pointer to a 3 element array of int".
  53.  
  54. Now if the parens were omitted, then we would start from the array,
  55. and then p would be an array of pointers to int.
  56.  
  57. Here's another example:
  58.  
  59. const int * (*f)(int, char);
  60.  
  61. Read this as:
  62.  
  63. "f * (int, char) * 'const int'"
  64.  
  65. or "f is a pointer to a function taking an int and a char returning a
  66. pointer to a const int". Where there's an adjacent sequence of
  67. keywords - data types, modifiers and qualifiers, the order is not
  68. important, i.e. "const int" is the same as "int const".
  69.  
  70. As to why it crashes when you reference it - did you initialize the
  71. pointer to point to anything? I've attached a sample program that
  72. works just fine.
  73.  
  74. Cheers.
  75.  
  76.  
  77. ----
  78.  Clyde Smith-Stubbs       | HI-TECH Software,       | Voice: +61 7 3300 5011
  79.  clyde@hitech.com.au      | P.O. Box 103, Alderley, | Fax:   +61 7 3300 5246
  80. http://www.hitech.com.au  | QLD, 4051, AUSTRALIA.   | BBS:   +61 7 3300 5235
  81. ----------------------------------------------------------------------------
  82. FREE! Download our shareware (FREE for noncommercial use) MS-DOS C Compiler!
  83.              Point your Web browser at http://www.hitech.com.au/
  84.  
  85. begin 644 afile
  86. M(VEN8VQU9&4)/'-T9&QI8BYH/@T*(VEN8VQU9&4)/'-T9&EO+F@^#0IM86EN
  87. M*"D-"GL-"@EI;G0@*"IP*5LS73L-"@T*"7`@/2!M86QL;V,H<VEZ96]F("IP
  88. M*3L-"@T*"2@J<"E;,%T@/2`R,SL-"@EP<FEN=&8H(B@J<"E;,%T@/2`E9%QN
  89. 1(BP@*"IP*5LP72D[#0I]#0HV
  90. `
  91. end
  92.